/*
 *  Copyright 2012 SerGenis. All rights reserved.
 */
package com.sergenis.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URISyntaxException;
import java.util.Properties;

/**
 *
 * @author SerGenis
 */
public class PropertiesIO {

    public static Properties read(java.net.URL url)
	throws IOException {
	InputStream is = url.openStream();
	Properties props = new Properties();
	props.load(is);
	is.close();
	return props;
    }

    public static Properties read(File file)
	throws IOException {
	return read(file.toURI().toURL());
    }

    public static Properties readXML(java.net.URL url)
	throws IOException {
	InputStream is = url.openStream();
	Properties props = new Properties();
	props.loadFromXML(is);
	is.close();
	return props;
    }

    public static Properties readXML(File file)
	throws IOException {
	return readXML(file.toURI().toURL());
    }

    public static void write(Properties props, File file)
	throws FileNotFoundException, IOException {
	write(props, file, null);
    }

    public static void write(Properties props, File file, String comments)
	throws IOException {
	FileOutputStream ops = new FileOutputStream(file, false);
	props.store(ops, comments);
	ops.close();
    }

    public static void writeXML(Properties props, File file, String comments)
	throws IOException {
	OutputStream os = new FileOutputStream(file, false);
	props.storeToXML(os, comments);
	os.close();
    }

    public static void writeXML(Properties props, File file)
	throws IOException {
	writeXML(props, file, null);
    }
}
